package com.roytrack.hazelcast.distributed.data.structure.map.listener;


import com.hazelcast.config.Config;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.map.listener.EntryAddedListener;
import com.hazelcast.map.listener.EntryRemovedListener;
import com.hazelcast.map.listener.EntryUpdatedListener;
import com.hazelcast.query.SqlPredicate;

public class MapListenerWithPredicate {
  public static void main(String[] args) {
    Config config = new Config();
    config.setProperty("hazelcast.map.entry.filtering.natural.event.types", "true");
    HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
    IMap<String, String> map = hz.getMap("map");
    map.addEntryListener(new MyEntryListener(), new SqlPredicate("surname=smith"), true);
    System.out.println("Entry Listener registered");
  }

  static class MyEntryListener
          implements EntryAddedListener<String, String>,
          EntryUpdatedListener<String, String>,
          EntryRemovedListener<String, String> {

    @Override
    public void entryAdded(EntryEvent<String, String> event) {
      System.out.println("Entry Added :" + event);
    }

    @Override
    public void entryRemoved(EntryEvent<String, String> event) {
      System.out.println("Entry Removed :" + event);
    }

    @Override
    public void entryUpdated(EntryEvent<String, String> event) {
      System.out.println("Entry Updated :" + event);
    }
  }
}


